package com.example.mobilesafe.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import com.example.mobilesafe.R;
import com.example.mobilesafe.constant.ConstantValue;
import com.example.mobilesafe.utils.SharedPreferencesUtil;
import com.example.mobilesafe.utils.ToastUtil;

public class SetupFourActivity extends BaseSetupActivity {

    /**
     * CheckBox控件
     */
    private CheckBox cb_box;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setup_four);

        // 初始化UI
        initUI();
    }

    @Override
    protected void showNextPage() {
        boolean open_security = SharedPreferencesUtil.getBoolean(this, ConstantValue.OPEN_SECURITY, false);
        if (open_security){
            Intent intent = new Intent(getApplicationContext(), SetupOverActivity.class);
            startActivity(intent);
            finish();
            // 存储标识
            SharedPreferencesUtil.putBoolean(this, ConstantValue.SETUP_OVER,true);
            overridePendingTransition(R.anim.next_in_anim,R.anim.next_out_anim);
        }else {
            ToastUtil.show(getApplicationContext(),"请开启防盗保护");
        }
    }

    @Override
    protected void showPrePage() {
        Intent intent = new Intent(getApplicationContext(), SetupThreeActivity.class);
        startActivity(intent);
        finish();
        overridePendingTransition(R.anim.pre_in_anim,R.anim.pre_out_anim);
    }

//    /**
//     * 1.“下一页”按钮的点击方法
//     * @param view
//     */
//    public void nextPage(View view){
//        boolean open_security = SharedPreferencesUtil.getBoolean(this, ConstantValue.OPEN_SECURITY, false);
//        if (open_security){
//            Intent intent = new Intent(getApplicationContext(), SetupOverActivity.class);
//            startActivity(intent);
//            finish();
//            // 存储标识
//            SharedPreferencesUtil.putBoolean(this, ConstantValue.SETUP_OVER,true);
//            overridePendingTransition(R.anim.next_in_anim,R.anim.next_out_anim);
//        }else {
//            ToastUtil.show(getApplicationContext(),"请开启防盗保护");
//        }
//    }

//    /**
//     * 2.“上一页”按钮的点击方法
//     * @param view
//     */
//    public void prePage(View view){
//        Intent intent = new Intent(getApplicationContext(), SetupThreeActivity.class);
//        startActivity(intent);
//        finish();
//        overridePendingTransition(R.anim.pre_in_anim,R.anim.pre_out_anim);
//    }

    /**
     * 3.初始化UI
     */
    private void initUI() {
        cb_box = findViewById(R.id.cb_box);
        // 1.是否选中状态的回显
        boolean open_security = SharedPreferencesUtil.getBoolean(this, ConstantValue.OPEN_SECURITY, false);
        // 2.根据状态，修改checkbox后续的文字显示
        cb_box.setChecked(open_security);
        if (open_security){
            cb_box.setText("安全设置已开启");
        }else {
            cb_box.setText("安全设置已关闭");
        }
        // 3.点击过程中，checkbox状态的切换
        cb_box.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // 4.状态切换后的存储
                SharedPreferencesUtil.putBoolean(getApplicationContext(),ConstantValue.OPEN_SECURITY,isChecked);
                // 5.根据开启关闭状态，去修改显示的文字
                if (isChecked){
                    cb_box.setText("安全设置已开启");
                }else {
                    cb_box.setText("安全设置已关闭");
                }
            }
        });
    }
}